﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using yq.IBase;
using yq.Model.Logic;
using System.Collections;
using System.Data.Common;
using yq.IBase.Basis;
using Newtonsoft.Json;

namespace yq.Model.Base
{
    ///<summary>
    /// Log_User - 用户登录统计 - 对象基础类
    /// </summary>
    ///<typeparam name="T"></typeparam>
    public abstract class ILogUser<T> : ModelBase<LogUser> where T : LogUser, new()
    {
        /// <summary>
        /// 字段列表
        /// </summary>
        protected override List<string> GetColumns()
        {
            return new List<string>() { "LogId", "UserId", "LogTime", "Ip", "Other", "Version" };
        }

        /// <summary>
        /// 表名
        /// </summary>
        protected override string GetTableName()
        {
            return "Log_User";
        }


        /// <summary>
        /// 写入主键值
        /// </summary>
        /// <param name="Id">主键</param>
        public override void SetKeyValue(dynamic Id)
        {
            this.LogId = Id;
        }

        /// <summary>
        /// 获取主键值
        /// </summary>
        /// <returns></returns>
        public override dynamic GetKeyValue()
        {
            return LogId;
        }

        /// <summary>
        /// 为本对象赋值
        /// </summary>
        /// <param name="dy"></param>
        public override void SetValue(dynamic dy)
        {
            this.LogId = dy.LogId;
            this.UserId = dy.UserId;
            this.LogTime = dy.LogTime;
            this.Ip = dy.Ip;
            this.Other = dy.Other;
            this.Version = dy.Version;
        }

        #region 基本属性定义


        /// <summary>
        /// logid
        /// </summary>
        private String _LogId = "";

        /// <summary>
        /// logid
        /// </summary>
        public String LogId
        {
            get
            {
                return (this._LogId);
            }

            set
            {
                _LogId = value;
            }
        }


        /// <summary>
        /// 账号
        /// </summary>
        private String _UserId = "";

        /// <summary>
        /// 账号
        /// </summary>
        public String UserId
        {
            get
            {
                return (this._UserId);
            }

            set
            {
                _UserId = value;
            }
        }


        /// <summary>
        /// 登录时间
        /// </summary>
        private DateTime _LogTime = DateTime.MinValue;

        /// <summary>
        /// 登录时间
        /// </summary>
        public DateTime LogTime
        {
            get
            {
                return (this._LogTime);
            }

            set
            {
                _LogTime = value;
            }
        }


        /// <summary>
        /// 登录IP
        /// </summary>
        private String _Ip = "";

        /// <summary>
        /// 登录IP
        /// </summary>
        public String Ip
        {
            get
            {
                return (this._Ip);
            }

            set
            {
                _Ip = value;
            }
        }


        /// <summary>
        /// 其他信息
        /// </summary>
        private String _Other = "";

        /// <summary>
        /// 其他信息
        /// </summary>
        public String Other
        {
            get
            {
                return (this._Other);
            }

            set
            {
                _Other = value;
            }
        }


        #endregion 基本属性定义

        #region 扩展属性定义

        UserAccount _UserAccount;

        /// <summary>
        /// UserAccount 外键关联 UserId（账号）   
        /// </summary>
        [ForeignKey("UserId")]
        [JsonIgnore]
        public UserAccount UserAccount
        {
            get
            {
                if (_UserAccount == null && UserId != null)
                {
                    _UserAccount = new UserAccount().Load(UserId);
                }
                return _UserAccount;
            }
            protected set { _UserAccount = value; }
        }

        #endregion 扩展属性定义

        protected override void SetValue(LogUser info, DataRow row)
        {
            if (row == null)
            {
                info.IsLoad = false;
            }
            else
            {
                info.IsLoad = true;
                if (row["LogId"] != DBNull.Value)
                {
                    info.LogId = row["LogId"].ToString();
                }
                if (row["UserId"] != DBNull.Value)
                {
                    info.UserId = row["UserId"].ToString();
                }
                if (row["LogTime"] != DBNull.Value)
                {
                    info.LogTime = Convert.ToDateTime(row["LogTime"]);
                }
                if (row["Ip"] != DBNull.Value)
                {
                    info.Ip = row["Ip"].ToString();
                }
                if (row["Other"] != DBNull.Value)
                {
                    info.Other = row["Other"].ToString();
                }
                if (row["Version"] != DBNull.Value)
                {
                    info.Version = row["Version"].ToString();
                }
            }

        }

        protected override void GetDbParameters(DbCommand comm, LogUser info, DbHelper cn)
        {
            cn.AddInParameter(comm, "@LogId", DbType.String, ParameterTool.GetObj(info._LogId));
            cn.AddInParameter(comm, "@UserId", DbType.String, ParameterTool.GetObj(info._UserId));
            cn.AddInParameter(comm, "@LogTime", DbType.DateTime, ParameterTool.GetObj(info._LogTime));
            cn.AddInParameter(comm, "@Ip", DbType.String, ParameterTool.GetObj(info._Ip));
            cn.AddInParameter(comm, "@Other", DbType.String, ParameterTool.GetObj(info._Other));
            cn.AddInParameter(comm, "@Version", DbType.String, Guid.NewGuid().ToString());
            cn.AddInParameter(comm, "@_versionOdl", DbType.String, info._Version);
        }

    }
}